import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { query } from '/@/utils/params';
import { Account } from '../../system/account/account';
import { easyDate } from '/@/utils/formatTime';

export interface BranchVo {
	id: string;
	name: string; //
	tel: string; //
	contactName: string; // 联系人
	contactTel: string; // 联系电话
	dealerId: string;
	companyId: string;
	address: string; // 地址
	account: string; // 账号
	password: string; // 密码
}
export interface BranchDTO {
	id: string;
	name: string; //
	tel: string; //
	contactName: string; // 联系人
	contactTel: string; // 联系电话
	address: string; // 地址
	account: Account; // 账号
	regions: string[];
	password: string; // 密码
	createdAt: string;
	company: any;
	dealer: any;
}
export const useBranch = defineStore('branch', {
	actions: {
		async getBranchList(name: string, pageSize: number, pageNumber: number) {
			return service.get(query('/api/branch', { name, pageSize, pageNumber })).then((res) => {
				res.list = res.list.map((c: BranchDTO) => {
					return {
						...c,
						account: c.account.account,
						company: c.company.name,
						companyId: c.company.id,
						dealer: c.dealer.name,
						dealerId: c.dealer.id,
						createdAt: easyDate(c.createdAt),
					};
				});
				return res;
			});
		},

		async saveBranch(vo: BranchVo) {
			return service.post('/api/branch/create', vo);
		},
		/**
		 * 编辑
		 */
		async updateBranch(vo: BranchVo) {
			return service.patch(`/api/branch/${vo.id}`, vo);
		},

		async delete(id: string) {
			return service.delete(`/api/branch/${id}`);
		},
		async toggoleStatus(id: string, status: number) {
			return service.patch(`/api/branch/${id}/status?status=${status}`);
		},
	},
});
